package com.xmkj.bearuser.sort;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.common.mvp.BaseMvpFragment;
import com.common.mvp.BasePresenter;
import com.common.retrofit.bean.FirstSortBean;
import com.common.retrofit.bearusermethod.GetFirstSortMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.xmkj.bearuser.R;
import com.xmkj.bearuser.home.BearSearhActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/10/26.
 */
@SuppressLint("ValidFragment")
public class SortDetailsFrament extends BaseMvpFragment implements AdapterView.OnItemClickListener {
    @BindView(R.id.listview)
    ListView listView;
    @BindView(R.id.fragment_container)
    FrameLayout fragmentContainer;
    LinearLayout llSearch;
    /*ListView填充用*/
    private List<String> strs = new ArrayList<>();
    /* = {"常用分类", "服饰内衣", "宠物", "手机", "家用电器", "数码", "电脑办公",
            "个护化妆", "图书", "鞋靴"}*/

    private MyAdapter adapter;
    private ContentFragment myFragment;
    /*选中的item的位数号码*/
    public static int mPosition = 0;
    private FirstSortBean sortBean;


    public SortDetailsFrament() {
    }

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sort_details;
    }

    @Override
    protected void onViewCreated(View view) {
        listView = (ListView) view.findViewById(R.id.listview);
        fragmentContainer = (FrameLayout) view.findViewById(R.id.fragment_container);
        llSearch = view.findViewById(R.id.ll_search);

    }

    @Override
    protected void doLogicFunc() {
        gotoHttp();
        attachClickListener(llSearch);

    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == llSearch.getId()) {
            gotoActivity(BearSearhActivity.class);
        }
    }

    private void gotoHttp() {
  /*      CommonSubscriber<SortBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                sortBean = (SortBean) o;
                if(position==1){
                    List<SortBean.TotalBean> total = sortBean.total;
                    for (int i = 0; i <total.size() ; i++) {
                        strs.add(total.get(i).name);
                    }
                } else {
                    List<SortBean.CategoryBean.ChildBean> child = sortBean.category.get(position - 2).child;
                    if(child!=null){
                        for (int i = 0; i <child.size() ; i++) {
                            strs.add(child.get(i).name);
                        }
                    }

                }
                initView();

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });


        SortMethods.getInstance().indexSort(subscriber);
        rxManager.add(subscriber);*/
        CommonSubscriber<FirstSortBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                sortBean = (FirstSortBean) o;
       /*         if(position==1){
                    List<SortBean.TotalBean> total = sortBean.total;
                    for (int i = 0; i <total.size() ; i++) {
                        strs.add(total.get(i).name);
                    }
                } else {
                    List<SortBean.CategoryBean.ChildBean> child = sortBean.category.get(position - 2).child;
                    if(child!=null){
                        for (int i = 0; i <child.size() ; i++) {
                            strs.add(child.get(i).name);
                        }
                    }

                }*/

//                strs.add("为你推荐"); // 第一个位置的
                if (sortBean.category_one != null && sortBean.category_one.size() != 0) {
                    for (int i = 0; i < sortBean.category_one.size(); i++) {
                        strs.add(sortBean.category_one.get(i).c_name);
                    }
                }
                initView();

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        GetFirstSortMethods.getInstance().categoryOne(subscriber);
        rxManager.add(subscriber);
    }

    private void initView() {
        //传参
        adapter = new MyAdapter(context, strs);
        listView.setAdapter(adapter);
        //监听事件
        listView.setOnItemClickListener(this);
        //创建MyFragment对象
        myFragment = new ContentFragment(context);
        FragmentTransaction fragmentTransaction = getChildFragmentManager()
                .beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, myFragment);
        //通过bundle传值给MyFragment
        Bundle bundle = new Bundle();
        bundle.putInt(ContentFragment.TAG, mPosition);  //确定 第几个子条目  从0 开始
        bundle.putString(ContentFragment.TITLE, strs.get(0));  //确定 第几个子条目  从0 开始
//        bundle.putString(ContentFragment.TITLE, sortBean.category_one.get(position ).c_name);  //确定 第几个子条目  从0 开始
        bundle.putInt(ContentFragment.CID, sortBean.category_one.get(0).c_id);


    /*    if(position==1){
            bundle.putInt(ContentFragment.CID,sortBean.total.get(0).id );
        } else {
            if(sortBean.category.get(position-2).child.size()>0){
                bundle.putInt(ContentFragment.CID,sortBean.category.get(position-2).child.get(0).id );
            }
        }*/

        //确定上面的大分类  从 1  开始  （1,0） 加载 特殊的
        myFragment.setArguments(bundle);
        fragmentTransaction.commit();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //拿到当前位置
        mPosition = position;
        //即使刷新adapter
        adapter.notifyDataSetChanged();
        for (int i = 0; i < strs.size(); i++) {
            myFragment = new ContentFragment(context);
            FragmentTransaction fragmentTransaction = getChildFragmentManager()
                    .beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, myFragment);
            Bundle bundle = new Bundle();
            bundle.putInt(ContentFragment.TAG, mPosition);  //确定 第几个子条目  从0 开始
//            if(position==0){ // 普通情况
//                bundle.putString(ContentFragment.TITLE, "为您推荐");  //确定 第几个子条目  从0 开始
////                bundle.putInt(ContentFragment.CID,sortBean. );
//            } else {  //  1  0  2  1
            bundle.putString(ContentFragment.TITLE, sortBean.category_one.get(position ).c_name);  //确定 第几个子条目  从0 开始
            bundle.putInt(ContentFragment.CID, sortBean.category_one.get(position).c_id);
//            }

            myFragment.setArguments(bundle);
            fragmentTransaction.commit();
        }
    }

/*    @Override
    protected void setStatusBar() {
        super.setStatusBar();
        m_statusBar.setVisibility(View.GONE);
    }*/
}
